package com.skydroid.h30

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ScrollView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.skydroid.library.DAPR
import com.skydroid.library.DRPR
import com.skydroid.library.SocketAT

/**
 * 获取H30信号强度示例
 */
class SignalDisplayActivity : AppCompatActivity() {
    private var dataSB = StringBuffer()
    private val maxLenth = 1024 * 1024

    private var mSocketATGCS: SocketAT? = null
    private var scrollView: ScrollView? = null
    private var tvData: TextView? = null

    private var tv_gcs_signal:TextView? = null
    private var tv_gcs_rssi:TextView? = null
    private var tv_gcs_snr:TextView? = null
    private var tv_gcs_rsrp:TextView? = null

    companion object{
        fun start(context: Context){
            context.startActivity(
                Intent(context,
                    SignalDisplayActivity::class.java)
            )
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_signal_display)
        initView()
        initGCS()
    }

    private fun initView() {
        scrollView= findViewById(R.id.scrollView)
        tvData= findViewById(R.id.tvData)
        findViewById<View>(R.id.btnVersion).setOnClickListener {
            mSocketATGCS?.queryVersion()
        }
        findViewById<View>(R.id.btnStart).setOnClickListener {
            mSocketATGCS?.startReportGCS()
        }
        findViewById<View>(R.id.btnStop).setOnClickListener {
            mSocketATGCS?.stopReportGCS()
        }
        tv_gcs_signal = findViewById(R.id.tv_gcs_signal)
        tv_gcs_rssi = findViewById(R.id.tv_gcs_rssi)
        tv_gcs_snr = findViewById(R.id.tv_gcs_snr)
        tv_gcs_rsrp = findViewById(R.id.tv_gcs_rsrp)
    }

    private fun initGCS(){
        //初始化socket
        mSocketATGCS = SocketAT("192.168.144.66",5051)
        mSocketATGCS?.setConnectListener(object :SocketAT.ConnectListener{
            override fun success() {
                //连接成功 回调
            }

            override fun reConnect() {
                //重连
                mSocketATGCS?.startReportGCS()
            }

            override fun fail() {
                //连接失败 回调
            }
        })
        mSocketATGCS?.setATResultListener(object :SocketAT.ATResultListener{
            override fun receive(datas: ByteArray) {
                //原始串口数据
                Log.e("GCS-receive",String(datas))
                updateData(datas)
            }

        })
        // 目前数据主要来源于dapr,其中signalStrength为信号强度百分比
        mSocketATGCS?.setRadioParameterListener(object:SocketAT.RadioParameterListener{
            override fun onDrprListener(datas1: DRPR, datas2: DRPR, signalStrength: Int) {
                //解析后 的drpr数据
                Log.e("onDrprListener","index:${datas1.index},rssi:${datas1.rssi},rsrp:${datas1.rsrp},rsrq:${datas1.rsrq},snr:${datas1.snr}")
                Log.e("onDrprListener","index:${datas2.index},rssi:${datas2.rssi},rsrp:${datas2.rsrp},rsrq:${datas2.rsrq},snr:${datas2.snr}")
            }

            override fun onDaprListener(datas1: DAPR, datas2: DAPR, signalStrength: Int) {
                //解析后 的dapr数据
                Log.e("onDaprListener","index:${datas1.index},rssi:${datas1.rssi},rsrp:${datas1.rsrp},rsrq:${datas1.rsrq},snr:${datas1.snr}")
                Log.e("onDaprListener","index:${datas2.index},rssi:${datas2.rssi},rsrp:${datas2.rsrp},rsrq:${datas2.rsrq},snr:${datas2.snr}")
                runOnUiThread {
                    tv_gcs_signal?.text = "${signalStrength}%"
                    tv_gcs_rssi?.text = "${datas1.rssi}dBm,${datas2.rssi}dBm"
                    tv_gcs_rsrp?.text = "${datas1.rsrp},${datas2.rsrp}"
                    tv_gcs_snr?.text = "${datas1.snr},${datas2.snr}"
                }
            }
        })
        //开启连接
        mSocketATGCS?.openConnect()

    }

    fun updateData(datas: ByteArray) {
        if(dataSB!!.length >= maxLenth){
            dataSB = StringBuffer()
        }
        dataSB?.append("Receive:")
        dataSB?.append(String(datas))
        dataSB?.append("\n")
        runOnUiThread {
            tvData?.text = dataSB?.toString()
            scrollView?.fullScroll(ScrollView.FOCUS_DOWN)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        //关闭连接
        mSocketATGCS?.stopReportGCS()
    }
}